package ca.etsmtl.log430.lab2;

/**
 * This class is used by various other classes that need to keep a list of
 * students on hand. It extends the List class which provides the basic
 * functionality for storage and retrieval of the Student Object from the list.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-02
 */

/*
 * Modification Log
 * ***************************************************************************
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 * 
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS.
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.
 * **************************************************************************
 */

public class StudentList extends List {

	/**
	 * Adds a new student to the list. All the issues of casting are taken care
	 * of within this class.
	 * 
	 * @param StudentObject
	 */
	public void addStudent(Student StudentObject) {

		appendItemToList((Object) StudentObject);

	} // AddStudent

	/**
	 * @return The student pointed at the current position pointed to by the
	 *         internal list pointer. Subsequent calls will return the next
	 *         student object in the list. A null object is returned if list is
	 *         empty or the end of list has been reached.
	 */
	public Student getNextStudent() {

		return (Student) getItemFromList();

	} // GetNextStudent

	/**
	 * Determines whether the Student OBJ is currently in the student list.
	 * 
	 * @param StudentObject
	 * @return true if the student is in the list, false otherwise.
	 */
	public boolean findStudent(Student StudentObject) {

		Student CurrentObject;
		boolean Done = false;
		boolean Result = false;

		// Note that this method assumes that all students have different
		// identification numbers.

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextStudent();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if (StudentObject.getStudentID().compareTo(
						CurrentObject.getStudentID()) == 0) {

					Result = true;

				} // if

			} // if

		} // while

		return (Result);

	} // FindStudent

	/**
	 * Finds a student in a list using the StudentID as the search key.
	 * 
	 * @param StudentID
	 * @return if the current list object's StudentID matches the StudentID
	 *         passed to the method, the Student object is returned to the
	 *         caller. Otherwise, returns null.
	 */
	public Student findStudentByID(String StudentID) {

		Student CurrentObject = null;
		boolean Done = false;
		boolean Found = false;

		// Note that this method assumes that all courses have different
		// identification numbers.

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextStudent();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if (CurrentObject.getStudentID().compareTo(StudentID) == 0) {

					Found = true;
					Done = true;

				} // if

			} // if

		} // while

		if (Found) {

			return (CurrentObject);

		} else {

			return (null);

		} // if

	} // FindStudentByID

} // StudentList
